# Contributor: Fabian Affolter <fabian@affolter-engineering.ch>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
#
# TODO: Unbundle gombo-parser.
pkgname=ruby-nokogiri
_gemname=${pkgname#ruby-}
pkgver=1.16.0
pkgrel=0
pkgdesc="An HTML, XML, SAX, and Reader parser"
url="https://nokogiri.org/"
arch="all"
license="MIT"
depends="
	ruby
	ruby-racc"
makedepends="
	libxml2-dev
	libxslt-dev
	ruby-dev
	ruby-mini_portile2
	"
checkdepends="ruby-minitest ruby-test-unit"
source="https://github.com/sparklemotion/$_gemname/archive/v$pkgver/$_gemname-$pkgver.tar.gz
	gemspec-fix-deps.patch
	test-helper-remove-unnecessary-deps.patch
	shutdown-libxml2-warning.patch
	test-skip-zip.patch
	"
builddir="$srcdir/$_gemname-$pkgver"

# secfixes:
#   1.13.10-r0:
#     - CVE-2022-23476
#   1.13.6-r0:
#     - CVE-2022-29181
#   1.13.4-r0:
#     - CVE-2022-24836
#   1.11.6-r0:
#     - CVE-2020-26247
#   1.10.4-r0:
#     - CVE-2019-5477

prepare() {
	default_prepare

	case "$CARCH" in
		# NotImplementedError: Compaction isn't available on this platform.
		ppc64le) rm test/test_compaction.rb ;;
	esac
}

build() {
	gem build $_gemname.gemspec
	gem install \
		--local \
		--install-dir dist \
		--ignore-dependencies \
		--no-document \
		--verbose \
		$_gemname-$pkgver.gem -- \
			--use-system-libraries

	# Remove unnecessary files and rubbish...
	find dist/extensions/ -name mkmf.log -delete
	cd dist/gems/$_gemname-$pkgver/
	rm -rf ext/ patches/
	rm ./*.md ./*.yml
	rm -rf lib/nokogiri/jruby/  # TODO: move into subpackage?
	rm lib/nokogiri/*.so
	rm lib/nokogiri/css/*.rex lib/nokogiri/css/*.y
}

check() {
	local extpath=$(dirname dist/extensions/*/*/$_gemname-$pkgver/gem_make.out)

	case "$CARCH" in
		# Fix NotImplementedError: Compaction isn't available on this platform.
		ppc64le) export NOKOGIRI_TEST_GC_LEVEL=major;;
	esac

	# Note: This is based on Fedora package.
	LANG="C.UTF-8" RUBYLIB=".:dist/gems/$_gemname-$pkgver/lib:test:$extpath" ruby \
		-e 'require "test/helper"' \
		-e 'Dir.glob("test/**/test_*.rb") {|f| require f }'
}

package() {
	local gemdir="$pkgdir/$(ruby -e 'puts Gem.default_dir')"

	cd "$builddir"/dist

	mkdir -p "$gemdir"
	cp -r bin extensions gems specifications "$gemdir"/
}

sha512sums="
0f6740ce69159f878844f2afdddafcc8c8f3bdabee68e5fcedb1526ae9cc48648c6ebda2a272ab1b8776fbde766bae97046802573908bd908cfb908288607e51  nokogiri-1.16.0.tar.gz
b4d2457a6065d6c0878841acd259d6e231bb08b3aa0868943d0f90733f211cfbca28fac3c9678d1874b8e6f91c746c0c4381211efaf9781267c9183197fc1912  gemspec-fix-deps.patch
4e637fccb39e4f5e7c4b2b902fc7ad60287a571fa37ab3b7afafbd3fdb7445a81518f654bf83e0c43b3e5ff06b37518f433fa22dc9f78e1cd840055603c55319  test-helper-remove-unnecessary-deps.patch
63519d5b314c6fee49c219c8ab6999b340e1e6b1acecfaf3aee3dcc37302be8ff82dbd00484b3570f457d5de3c54529b843e1d382f5b65b9e128548b37c97198  shutdown-libxml2-warning.patch
ab9681d88bfa0939134e333db751c2564629667ff7d49a5db93c3f49b88bc08349eae32d3a35c9235bb76b27b259033f949ad1ad5d9cf047b2eb8d2546632b7b  test-skip-zip.patch
"
